<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="author" content="lushu">
		<title>备用金支票充值</title>
		<script language="javascript" type="text/javascript" src="../script/print.js"></script>
		<script language="javascript" src="../script/common.js" type="text/javascript"></script>
		<script language="javascript" src="../script/card.js" type="text/javascript"></script>
		<script language="javascript" type="text/javascript">
			var DEFAULT_MAX_ITEM = 20;
			var DEFAULT_ADD_ITEM = 4;
			var ALLOW_DUPLICATED_DEPOSIT = false;
			var Page;
			
			function load() {
				Page = {};
				Page.accCode = AccountType.FUND;
				Page.amountInSystem = Account.Transformer.InByAccCode(Page.accCode);
				Page.count = 0;
				Page.deposits = new Array();
				Page.depositForm = $('depositForm');
				Page.depositForm.allowDup.checked = ALLOW_DUPLICATED_DEPOSIT;
				Page.chequeForm = $('chequeForm');
				Common.initDict(Page.chequeForm.bank, "bank", "请选择");
				addMoreDeposit(1);
				Eui.showNavigation();
				setPages(["operationDiv","billDiv"]);
				initActiveX();
			}
			//reset forms
			function resetAll(flag){
				if(flag || showQuestion("确认放弃已经输入的信息？")){
					Page.depositForm.operation.disabled = false;
					Page.depositForm.reset();
					Page.chequeForm.reset();
					clearErrorView(Page.chequeForm.id);
					$("depositList").innerHTML = "";
					Page.count = 0;
					Page.deposits = new Array();
					addMoreDeposit(1);
				}
			}
			function goFirstPage(){
				resetAll(true);
				switchPage("operationDiv");
			}
			//检查支票能否创建
			function checkCheque(){
				$("chequeInfo").innerHTML = "";
				if (Page.chequeForm.bank.value && Page.chequeForm.chequeNo.value){
					DWR.call("tradeService.checkDuplicatedCheque",
						Page.chequeForm.chequeNo.value,
						Page.chequeForm.bank.value,
						function(result){
							if(result)
								$("chequeInfo").innerHTML = "◎该支票已存在,无法再用来充值";
						}
					);
				}
			}
			//检查单个充值的CardUser是否有效
			function checkSingleDepoist(i){
				$("deposit"+i).innerHTML = "";
				if (Page.depositForm["asn"+i].value || Page.depositForm["cardUserUnique"+i].value){
					try{
						var result = DWR.syncCall("tradeService.precheckDeposit",
								Page.depositForm["asn"+i].value,
								Page.depositForm["cardUserUnique"+i].value);
						Page.deposits[i]={uniqueId:result,status:1};
						$("deposit"+i).innerHTML = "√";
					}catch(e){
						Page.deposits[i]={status:-1};
						$("deposit"+i).innerHTML = e.message;
					}
				}else{
					delete(Page.deposits[i]);
				}
				mark:
				for(var i=1;i<=Page.count;i++)
					if (Page.deposits[i] && Page.deposits[i].status!=-1){
						for(var j=1;j<=Page.count;j++)
							if (j!=i && Page.deposits[j] && Page.deposits[j].uniqueId == Page.deposits[i].uniqueId){
								Page.deposits[i].status = 0;
								$("deposit"+i).innerHTML = "重复的客户";
								continue mark;
							}
						Page.deposits[i].status = 1;
						$("deposit"+i).innerHTML = "√";
					}
				refreshDuplicate();
			}
			//刷新重复客户的提示
			function refreshDuplicate(){
				for(var i=1;i<=Page.count;i++)
					if (Page.deposits[i] && Page.deposits[i].status===0)
						if (Page.depositForm.allowDup.checked)
							$("deposit"+i).innerHTML = "√";
						else
							$("deposit"+i).innerHTML = "重复的客户";
			}
			//获取一个充值条目的html代码
			function getDepositInput(i){
				var str = "<table cellSpacing='0' cellPadding='4' width='100%' border='0'><tr><td>"
						+ "<label>卡&nbsp;&nbsp;&nbsp;&nbsp;号<\/label> "
						+ "<input type='text' size='22' name='asn#' value='' maxlength='16' onblur=\"checkV(this,'asn');\" onchange=\"checkSingleDepoist(#);\">"
						+ "<button onclick=\"Page.depositForm.asn#.value=TX.Trade.readAsn();checkV(Page.depositForm.asn#,'asn');checkSingleDepoist(#);\">读 卡<\/button>"
						+ "\/<label>客户编号<\/label> "
						+ "<input type='text' size='22' name='cardUserUnique#' value='' maxlength='18' onblur=\"checkV(this,'uniqueId')\" onchange=\"checkSingleDepoist(#)\">"
						+ "<span class='des' id='deposit#'></span>"
						+"</td><td width='20%'>"
						+ "<label>充 值 额<\/label> "
						+ "<input type='text' name='amount#' size='8' onblur=\"checkV(this,'positiveAmount')\" onchange='refreshTotalAmount()'>"
						+"<\/td><\/tr><\/table>";
				return str.replace(/#/g,i);
			}
			//添加更多的充值条目
			function addMoreDeposit(number){
				if (Page.count>=DEFAULT_MAX_ITEM){
					showWarning("充值记录超过最大限制");
					return;
				}
				if (!number){
					if (Page.count<DEFAULT_ADD_ITEM)
						number = Page.count;
					else 
						number = DEFAULT_ADD_ITEM;
					if (number+Page.count>DEFAULT_MAX_ITEM)
						number = DEFAULT_MAX_ITEM - Page.count;
				}
				var str = $("depositList").innerHTML;
				for(var i=0;i<number;i++){
					Page.count++;
					str += getDepositInput(Page.count);
				}
				$("depositList").innerHTML = str;
			}
			//获取充值总额
			function refreshTotalAmount(){
				var amount = 0;
				for (var i =1;i<=Page.count;i++)
					if (Page.depositForm["amount"+i].value)
						amount += (Page.depositForm["amount"+i].value-0)*100;
				Page.depositForm.totalAmount.value = amount/100;
				if (Page.chequeForm.chequeAmount)
					Page.depositForm.chequeBalance.value = ((Page.chequeForm.chequeAmount.value-0)*100-amount)/100;
			}
			function doDeposit(){
				if ($("chequeInfo").innerHTML != "")
					return;
				if(!checkForm(Page.chequeForm.id))
					return;
				if (!checkForm(Page.depositForm.id))
					return;
				//isCheckForm = true;
				var list = [];
				for (var i =1;i<=Page.count;i++){
					if (Page.deposits[i]
							&& (Page.deposits[i].status==-1 
									|| (Page.deposits[i].status===0 && !Page.depositForm.allowDup.checked)))
						return;
					if (Page.depositForm["asn"+i].value || Page.depositForm["cardUserUnique"+i].value){
//						if (!checkV(Page.depositForm["amount"+i],'positiveAmount',true)){
//						}
						if (Page.depositForm["amount"+i].value==""){
							$("deposit"+i).innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入充值金额:";
							return;
						}else{
							$("deposit"+i).innerHTML = "√";
						}
					}
					else if (Page.depositForm["amount"+i].value!=''){
						if (!TX.Misc.hasAny([Page.depositForm["asn"+i],Page.depositForm["cardUserUnique"+i]])){
							$("deposit"+i).innerHTML = "卡号或客户编号必须输入其一";
							return;
						}
					}else{
						normalStyle(Page.depositForm["amount"+i]);
						$("deposit"+i).innerHTML = "";
						delete(Page.deposits[i]);
					}
					if (Page.deposits[i]){
//						var dep = {};
//						dep.uniqueId = Page.deposits[i].uniqueId;
//						dep.amount = Page.amountInSystem(Page.depositForm["amount"+i].value);
						var dep = Page.deposits[i].uniqueId+','+Page.amountInSystem(Page.depositForm["amount"+i].value);
						list.push(dep);
					}
				}
				//isCheckForm = false;
				if (list.length==0){
					showWarning("请输入充值信息");
					return;
				}
				if (Page.chequeForm.chequeAmount.value!=Page.depositForm.totalAmount.value){
					showWarning("充值总金额和支票金额不符");
					return;
				}
				DWR.call("tradeService.depositWithCheque",
					Page.chequeForm.bank.value,
					Page.chequeForm.chequeNo.value,
					Page.amountInSystem(Page.chequeForm.chequeAmount.value),
					Page.accCode,
					list,
					function (result){
						showInformation("充值成功");
						//显示凭证
						Eui.getBillHtml('content',{queryName:'cheque_deposit',chequeId:result});
						switchPage("billDiv");
						WebPrint.print('content')
					},
					{disCtrl:Page.depositForm.operation}
				);
			}
		</script>
	</head>
	<body onload="load()">
		<div class="nav"><div id="nav">&nbsp;</div></div>
		
		<div id="operationDiv">
			<div class="form">
				<div class="formTitle">支票信息</div>
				<form id="chequeForm" name="chequeForm">
					<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label6">
						<tr>
							<td>
								<label>支票所属银行</label>
								<select name="bank" onblur="checkV(this,'',true);" onchange="checkCheque();"><option></option></select> <span class="des">*</span>
							</td>
							<td>
								<label>支 票 号</label>
								<input type="text" name="chequeNo" onblur="checkV(this,'long',true);" onchange="checkCheque();"> <span class="des">*</span>
							</td>
						</tr>
						<tr>
							<td>
								<label>支票金额(元)</label>
								<input type="text" name="chequeAmount" onblur="checkV(this,'positiveAmount',true);" onchange="refreshTotalAmount();"> <span class="des">*</span>
							</td>
							<td><div id="chequeInfo" class="des"></div></td>
						</tr>
					</table>
				</form>
			</div>
			
			<form id="depositForm" name="depositForm">
				<div class="form">
					<div class="formTitle">备用金帐户充值</div>
					<table cellSpacing="0" cellPadding="4" width="100%" border="0">
						<tr>
							<td>
								充值总金额 <input type="text" name="totalAmount" class="readonly" size="8" readonly> 元
							</td>
							<td>
								支票可用余额 <input type="text" name="chequeBalance" class="readonly" size="8" readonly> 元
							</td>
							<td>
								<input type="checkbox" name="allowDup" onclick="refreshDuplicate();">允许给同一客户重复充值
							</td>
						</tr>
					</table>
				</div>
			
				<div class="form">
					<div class="formTitle">备用金帐户充值-明细信息</div>
					<div id="depositList"></div>
				</div>
				<p>
					<button onclick="addMoreDeposit()">添加更多充值信息</button>
					<button id="operation" onclick="doDeposit()">充 值</button>
					<button onclick="resetAll()">重 置</button>
				</p>
			</form>
		</div>
			
		<div id="printSheetDiv" style="display:none;" >
			<div class="form">
				<div class="formTitle">充值明细</div>
				<div id="mainTrade"></div>
			</div>
			<p>
				<button onclick="TX.NoCardTrade.goFirstPage()">返 回</button>
			</p>
		</div>
		<div id="billDiv" class="form" width="100% " style="display:none;">
			<div class="formTitle">
				<div class="l">交易凭证</div>
				<div class="r">
					<button onclick="WebPrint.preview('content')">打印预览</button>
			 		<button onclick="WebPrint.print('content')">打印</button>
			 		<button onclick="goFirstPage()">返 回</button>
				</div>
			</div>
			<div  id="content"  align='center'></div>
		</div>
	</body>
</html>